<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-9-3
 * Time: 下午4:49
 * To change this template use File | Settings | File Templates.
 */
class msg extends MY_Controller
{
    public function index()
    {
        $this->load->helper('url');

        $Limit = 20;
        $currentPage = $this->uri->rsegment(3, 1);
        $offset = ($currentPage - 1) * $Limit;

//error_reporting(E_ALL);

        $this->load->model('messages');
        $totalNum = $this->messages->getMessageCount();

        $data = $this->messages->getMessage('*', $Limit, $offset, null, 'create_time desc');

        $pageHtml = '';
        if($totalNum > $Limit)
        {
            $this->load->library('pagination');
            $config['base_url'] = '/msg/index/';
            $config['total_rows'] = $totalNum;
            $config['per_page'] = $Limit;
            $config['num_links'] = 8;
            $config['uri_segment'] = 3;
            $config['first_link'] = '首页';
            $config['last_link'] = '尾页';
            $config['use_page_numbers'] = TRUE;
            $config['cur_tag_open'] = '<a id="cur">';
            $config['cur_tag_close'] = '</a>';
            $config['prev_link'] = '上一页';
            $config['next_link'] = '下一页';
            $this->pagination->initialize($config);
            $pageHtml = $this->pagination->create_links();
        }

        $vData = array(
            'title' => '留言求片',
            'page_html' => $pageHtml,
            'data' => $data,
            'keywords' => '电影1010 留言求片',
            'description' => '电影1010 全网最全资源库',
        );

        $this->load->view('message', $vData);
    }

    public function submit()
    {
        header("Content-type: text/html; charset=utf-8");

        $userName = $this->input->get_post('username');
        $content = $this->input->get_post('content');
        $ip = $this->input->ip_address();

        if (empty ($content)) {
            echo '<script>alert("留言内容为空!");</script>';exit;
        }

        $info = array(
            'username' => $userName,
            'content' => $content,
            'ip' => $ip,
            'create_time' => date('Y-m-d H:i:s'),
        );
        $this->load->model('messages');
        $this->messages->save($info);

        echo '<script>alert("留言成功!");window.location.href="/msg/index";</script>';exit;
    }
}
